package com.shire.shire_interface.service.validation;

import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.dao.validation.WxValidationRuleDao;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.validation.WxValidationRule;
import com.shire.shire_interface.model.validation.WxValidationRuleExample;
import com.shire.shire_interface.service.commandcenter.BaseCommandProcessor;
import com.shire.shire_interface.service.commandcenter.WxCommandService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@CacheConfig(cacheNames = {"myCache"})
public class WxValidationRuleService {

    @Resource
    private WxValidationRuleDao wxValidationRuleDao;
    @Resource
    private WxCommandService commandService;
    @Resource
    private BaseCommandProcessor commandProcessor;

    public List<WxValidationRule> selectByExample(WxValidationRuleExample example) {
        return wxValidationRuleDao.selectByExample(example);
    }

    @CachePut(key = "targetClass+''+ #wxValidationRule.csid")
    public WxValidationRule insert(WxValidationRule wxValidationRule) {
         wxValidationRuleDao.insertSelective(wxValidationRule);
         return wxValidationRule;
    }
    @CachePut(key = "targetClass+''+ #wxValidationRule.csid")
    public WxValidationRule update(WxValidationRule wxValidationRule){
        wxValidationRuleDao.updateByPrimaryKey(wxValidationRule);
        return wxValidationRule;
    }

    public void syncValidationList(Integer csId, Integer accountId){
        WxCommand command = commandService.generateCmdObject(csId,accountId,"{}",WxConstants.CmdTaskType.UNKNOWN,-1,WxConstants.CmdType.CRK_HELLO_FETCH_D,WxConstants.CmdPriority.UNKNOWN);
        commandProcessor.addCommand(csId,accountId,command);
    }

    @Cacheable(key = "targetClass +''+ #p0")
    public WxValidationRule getValidationRuleForCs(String wxId){
        WxValidationRuleExample example = new WxValidationRuleExample();
        example.createCriteria().andWxaccountEqualTo(wxId);

        List<WxValidationRule> ruleList = wxValidationRuleDao.selectByExample(example);
        if (ruleList!= null && ruleList.size()>0)
            return ruleList.get(0);
        return null;
    }
}
